/*
ApolloOS 0.1 - Bob Moss - bobbyjmoss@gmail.com
Derived from MikeOS 1.4.2 and bkerndev
This project is licensed under GPLv3 (see 'doc/gpl.txt'and 'doc/license.txt' for further details) 
All acknowledgements are available in 'CREDITS.TXT'. 
(C)2008 ApolloOS developers
*/

/* The Timer module */

#include <system.h>

/* This will keep track of how many ticks that the system
*  has been running for */
int timer_ticks = 0;
int output_limit = 0;

/* Handles the timer. In this case, it's very simple: We
*  increment the 'timer_ticks' variable every time the
*  timer fires. By default, the timer fires 18.222 times
*  per second. Why 18.222Hz? Some engineer at IBM must've
*  been smoking something funky */
void timer_handler(struct regs *r)
{
    /* Increment our 'tick count' */
    timer_ticks++;
	
	/*This if() statement ensures that it doesn't output forever (even if it does carry on counting! */
	if (output_limit < 10)
	{
    /* Every 18 clocks (approximately 1 second), we will
    *  display a message on the screen */
    if (timer_ticks % 18 == 0)
    {
        puts("One Second Has Passed...\n");
	output_limit = output_limit + 1;
    }
	}
	else if(output_limit == 10)
	{
		puts("All Done! Now for an eternal loop...\n");
		output_limit = output_limit + 1;
	}
	else if(output_limit > 10)
	{
		output_limit = output_limit + 1;
		/* This provision is in so in future ApolloOS releases I can tell the user how long their session has lasted or to set a delay on launching the GUI */
	}
}

/* This will continuously loop until the given time has
*  been reached */
void timer_wait(int ticks)
{
    unsigned long eticks;

    eticks = timer_ticks + ticks;
    while(timer_ticks < eticks);
}

/* Sets up the system clock by installing the timer handler
*  into IRQ0 */
void timer_install()
{
    /* Installs 'timer_handler' to IRQ0 */
    irq_install_handler(0, timer_handler);
}
